package com.greateamd.timetableasy.client.widgets;

import com.google.gwt.dom.client.Document;
import com.google.gwt.event.dom.client.ChangeEvent;

public class ListBox extends com.google.gwt.user.client.ui.ListBox 
{
	public ListBox() {
		super(true);
	}
	public ListBox(boolean multi) {
		super(multi);
	}
	
	@Override
	public void insertItem(String item,int index)
	{
		super.insertItem(item, index);
		ChangeEvent.fireNativeEvent(Document.get().createChangeEvent(), this);
	}
	
	@Override
	public void removeItem(int index)
	{
		super.removeItem(index);
		ChangeEvent.fireNativeEvent(Document.get().createChangeEvent(), this);
	}
	
	@Override
	public void addItem(String item) {
		if(!contains(item)){
			super.addItem(item);
		}
	}
	
	public boolean addItemIfNotPresent(String item) {
		if(!contains(item)){
			super.addItem(item);
			return true;
		}
		return false;
	}

	public boolean contains(String item) {
		for(String s :getValues()){
			if(s.equals(item)) return true;
		}
		return false;
	}
	
	public String getSelectedValue(){
		if(getSelectedIndex() > -1)
			return getItemText(getSelectedIndex());
		else
			return null;
	}
	
	public String[] getValues(){
		String[] values = new String[getItemCount()];
		for(int i = 0 ; getItemCount() > i ; i++){
			values[i] = getItemText(i);
		}
		return values;
	}
}
